﻿/*****************************************************************************\
 *             University of Illinois/NCSA Open Source License
 *
 * 
 *      Copyright © 2012,2013, The Board of Trustees of the University of Illinois. All rights reserved.
 *
 *      Developed by: Rakesh Bobba, Derek Dagit, Erich Heine, Denis Kholine,
 *                    Klara Nahrstedt, David Rodgers, William H. Sanders, Tim Yardley
 *
 *      Information Trust Institute/Coordinated Science Laboratory
 * 
 *      University of Illinois
 *      
 *      The Board of Trustees of the University of Illinois
 *      109 Coble Hall
 *      801 South Wright Street
 *      Champaign, IL 61820-6242
 *
 *      http://www.iti.illinois.edu / http://csl.illinois.edu
 * 
 *      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 *      associated documentation files (the “Software”), to deal with the Software without restriction, 
 *      including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *      and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 *      subject to the following conditions:
 *
 *      Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *      following disclaimers.
 *      
 *      Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 *      the following disclaimers in the documentation and/or other materials provided with the distribution.
 *
 *      Neither the names of Information Trust institute, Coordinated Science Laboratory, University of Illinois,
 *      The Board of Trustees of the University of Illinois nor the names of its contributors may be used to 
 *      endorse or promote products derived from this Software without specific prior written permission.
 *      
 *      THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 *      LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN
 *      NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *      WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 *      SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 *      
 \*****************************************************************************/

using System;
using System.Timers;

namespace SIEGate.Core.Stats
{
    /// <summary>
    /// Represents stats-related events that should fire.
    /// </summary>
    // Different times to fire:
    // * Every N get/reset stats (for a given stat)
    // * Time aggregate methods
    // * StatInstanceFire event
    // Track what to do when one of the above fires.
    // Common actions:  Log, send to StatMapper.
    public class Interval
    {
        private readonly Timer m_timer;

        public static Interval Instance = new Interval();
        private const double DefaultInterval = 60000;

        public delegate void IntervalElapsedEventHandler(object sender, EventArgs e);

        private Interval()
        {
            m_timer = new Timer {AutoReset = true, Interval = DefaultInterval, Enabled = true};
            m_timer.Elapsed += OnIntervalElapsed;
        }

        public void StartTimer()
        {
            m_timer.Enabled = true;
        }

        public void StopTimer()
        {
            m_timer.Enabled = false;
        }

        public double TimerInterval
        {
            get { return m_timer.Interval; }
            set { m_timer.Interval = value;  }
        }

        // Note: For future functionality, because we may want to update certain things on a timescale
        public event IntervalElapsedEventHandler StatUpdaters;
        public event IntervalElapsedEventHandler StatOutputters;

        private void OnIntervalElapsed(object sender, ElapsedEventArgs e)
        {
            // Note: this order sort of matters - be careful about changing it.
            if (StatUpdaters != null)
            {
                StatUpdaters(sender, e);
            }

            Stats.StatDispatcher.IntervalElapsed();
            
            if (StatOutputters != null)
            {
                StatOutputters(sender, e);
            }
        }

        public void ClearStatOutputtersAndUpdaters()
        {
            StatOutputters = null;
            StatUpdaters = null;
        }
    }
}
